import { Controller, Get, Post, Body, Headers, Param, Delete, HttpCode, Query, UseGuards } from '@nestjs/common';
import { OrderService } from './order.service';
import { AuthGuard } from '@nestjs/passport';
import { ApiResponse, ApiOperation, ApiBody, ApiQuery, ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CreateOrderDto, GetOrderDto, UpdateOrderDto } from './dto/order.dto';
import { OrderResult } from './entities/order.entity';
import { TokenDto } from 'src/common/auth/constants';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';
import { JwtService } from '@nestjs/jwt';
@ApiBearerAuth() // swagger文档设置token
@ApiTags('订单')
@Controller('admin/order')
export class OrderController {
  constructor(private readonly orderService: OrderService,
    private readonly jwtService: JwtService
  ) { }
  /** 订单 */
  @HttpCode(200)
  @Get('/getOrderPage')
  @ApiResponse({
    status: 200,
    description: '订单列表',
    type: OrderResult,
  })
  @ApiOperation({
    tags: ['获取订单'],
    summary: '获取订单',
    operationId: 'getOrderPage',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  getOrderPage(@Query() params: GetOrderDto) {
    return this.orderService.getOrderPage(params);
  }
  @HttpCode(200)
  @Post('/createOrder')
  @ApiBody({
    description: '创建订单',
    type: CreateOrderDto,
  })
  @ApiOperation({
    summary: '创建订单',
    operationId: 'createOrder',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async createOrder(@Body() body: CreateOrderDto, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.orderService.createOrder(body, user);
  }
  @HttpCode(200)
  @Post('/updateOrder')
  @ApiBody({
    description: '更新订单信息',
    type: UpdateOrderDto,
  })
  @ApiOperation({
    summary: '更新订单信息',
    operationId: 'updateOrder',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async updateOrder(@Body() body: UpdateOrderDto, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.orderService.updateOrder(body, user);
  }
}
